BEGIN TRANSACTION;  -- 开启事务‌:ml-citation{ref="2,6" data="citationList"}
BEGIN TRY
    -- 扣减账户A余额（UPDLOCK防止并发修改）
    UPDATE Accounts WITH (UPDLOCK)
    SET Balance = Balance - 1000
    WHERE AccountID = 'A001' AND Balance >= 1000;

    -- 增加账户B余额
    UPDATE Accounts 
    SET Balance = Balance + 1000
    WHERE AccountID = 'B002';

    COMMIT TRANSACTION;  -- 提交事务‌:ml-citation{ref="2,7" data="citationList"}
END TRY
BEGIN CATCH
    IF XACT_STATE() = -1 
        ROLLBACK TRANSACTION;  -- 强制回滚‌:ml-citation{ref="2,4" data="citationList"}
    -- 记录错误日志
    INSERT INTO ErrorLog (Message) VALUES (ERROR_MESSAGE());
    THROW;  -- 抛出异常‌:ml-citation{ref="4,7" data="citationList"}
END CATCH;
